package io.renren.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import io.renren.dao.CountDao;
import io.renren.entity.Count;
import io.renren.service.CountService;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service("countService")
public class CountServiceImpl implements CountService {
    @Resource
    private CountDao countDao;

    @Override
    public Count queryObject(Long id) {
        return countDao.queryObject(id);
    }

    @Override
    public List<Count> queryList(Map<String, Object> map) {
        return countDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return countDao.queryTotal(map);
    }

    @Override
    public void save(Count count) {
            countDao.save(count);
    }

    @Override
    public void update(Count count) {
            countDao.update(count);
    }

    @Override
    public void delete(Long id) {
            countDao.delete(id);
    }

    @Override
    public void deleteBatch(Long[]ids) {
            countDao.deleteBatch(ids);
    }

    @Override
    @Transactional
    public void updateViews(Long videoId) {
        Count count = countDao.selectObject(videoId);
        log.debug("数据库视频观看记录总数为：{}",count);
        Map countMap = new HashMap();
        countMap.put("id",videoId);
        //2.2 如果不存在则创建
        if(count == null){
            log.debug("数据库不存在该视频的总点赞数");
            countMap.put("zan",0);
            countMap.put("viewNumber",1);
            countDao.save(countMap);
            log.debug("添加观看记录成功");
        }else {
            countDao.updateViewAdd(countMap);
            log.debug("增加观看记录成功");
        }
    }

}
